package cn.edu.wdu.gaojie.jdbc;

import java.sql.*;

/**
 * @author 高杰  gao4433@qq.com
 * @Description JDBC 工具类，支持增删改查，返回受影响行数或查询结果
 * @date 2021/12/3 13:48
 * @since 1.8
 */
public class JdbcUtils {

    /**
     * 更新方法
     * @param sql [in] 传入的 SQL 语句
     * @return 受影响的行数
     */
    public static int update(String sql) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        String userName = "root";
        String passWord = "778874315";

        Connection connection = DriverManager.getConnection(url, userName, passWord);

        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(sql);
        return i;
    }

    /**
     * 打印查询的结果
     */
    public static void select(String sql) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        String userName = "root";
        String passWord = "778874315";

        Connection connection = DriverManager.getConnection(url, userName, passWord);
        Statement statement = connection.createStatement();
        ResultSet set = statement.executeQuery(sql);

        while (set.next()) {
            System.out.println(set.getInt(1) + "   " + set.getString(2));
        }
    }

}
